<?php
/**
 * User: luchao
 * Createtime: 2016/10/24 13:58
 * desc：争议
 */
namespace AuctionBack\Controller;
use AuctionCore\System\AuctionException;
use AuctionCore\Dispute\Dispute;
use AuctionCore\AuctionOrder\AuOrder;
class DisputeController extends BaseController
{   
    public function __construct()
    {
        parent::__construct();
        $this->emp_id = $this->user_info['emp_id'];
    }

    public function index()
    {
        $this->display();
    }
    //列表
    public function getList()
    {
        $page = I("get.p", 1);
        $size = 10;
        $params = array();
        $params['status'] = I("get.status/d");
        $params['car_plate_number'] = trim(I("get.car_plate_number"));
        $params['search_key'] = trim(I("get.search_key"));
        // $params['originator'] = trim(I("get.originator"));
        $params['seller_buyer'] = I("get.seller_buyer",1);
        $location_area = I("get.location_area");
        if (!$location_area) {
            $location_area = $this->user_info['bc_code'];
        } else {
            $location_area = [$location_area];
        }
        $params['location_area'] = $location_area;
        //获取list
        $result = Dispute::DisputeList($page, $size, $params);
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show(); // 分页显示输出
        //模板赋值
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('list', $result['list']);
        $this->assign("params", $params);
        $this->display();
    }
    //审核
    public function verify(){
        $did = I('get.did');
        if ($did < 1) {
            $this->helpJsonResult(500, '争议id不能为空');
        }
        $dis = new Dispute($did);
        $disInfo = $dis ->detail();
        if ($disInfo['status'] > 1) {
            $this->helpJsonResult(500, '争议数据已处理');
        }
        $res = $dis->DisputeDetail();
        $dispute_reason = C('DISPUTE_REASON');
        $dispute_status = C('DISPUTE_STATUS');
        $this->assign('dispute_reason',$dispute_reason);
        $this->assign('dispute_status',$dispute_status);
        $modify_reason = C('MODIFY_REASON');
        $modify_status = C('MODIFY_STATUS');
        $this->assign('modify_reason', $modify_reason);
        $this->assign('modify_status', $modify_status);
        $this->assign('res',$res);
        if($res['od_type'] == 1){
            $this->display('Dispute/dealerVerify');
        }else{
            $this->display('Dispute/ownerVerify');
        }
    }

    public function detail(){
        $did = I('get.did');
        if ($did < 1) {
            $this->helpJsonResult(500, '争议id不能为空');
        }
        $dis = new Dispute($did);
        $res = $dis->dcDetail();
        $dispute_reason = C('DISPUTE_REASON');
        $dispute_status = C('DISPUTE_STATUS');
        $this->assign('dispute_reason',$dispute_reason);
        $this->assign('dispute_status',$dispute_status);
        $modify_reason = C('MODIFY_REASON');
        $modify_status = C('MODIFY_STATUS');
        $this->assign('modify_reason', $modify_reason);
        $this->assign('modify_status', $modify_status);
        $this->assign('res',$res);
        $this->display();
    }
    // 争议审核-通过
    public function disputePass(){
        $did = I('get.did');
        if ($did < 1) {
            $this->helpJsonResult(500, '争议id不能为空');
        }
        $dis = new Dispute($did);
        $disInfo = $dis ->detail();
        if ($disInfo['status'] > 1) {
            $this->helpJsonResult(500, '争议数据已处理');
        }
        $o = new AuOrder($disInfo['order_id']);
        $orderInfo = $o ->detail();
        // if ($orderInfo['status'] ==12) {
        //     $this->helpJsonResult(500, '争议数据异常');
        // }
        $data = [];
        // $is_having_last = I('post.is_having_last');
        // $last_mid = I('post.last_mid');
        $od_type = I('post.od_type');
        $modify_result = I('post.modify_result');
        $modify_reason = I('post.modify_reason');
        $modify_desc = I('post.modify_desc');
        $payment_amount = I('post.payment_amount/d',0); //车商赔付金额
        $account_name = I('post.account_name');
        $bank_account = I('post.bank_account');
        $bank_name = I('post.bank_name');
        $emp_id = $this->emp_id;
        
        if ($od_type == 1 && $modify_result == 1) {
            $data['dealer_compensate'] = $payment_amount;   
        }
        if ($od_type == 1 && $modify_result == 2) {
            $data['dealer_compensate'] = $payment_amount;   
        }
        if ($od_type == 2 && $modify_result == 1) {
            $data['owner_compensate'] = $payment_amount;   
        }
        //当车主争议->平台失误->订单关闭，车商车主都需要赔付
        if ($od_type == 2 && $modify_result == 2) {
            $owner_payment_amount = I('post.owner_payment_amount/d',0);
            $data['owner_payment_amount'] = $owner_payment_amount;
            $data['dealer_compensate'] = $payment_amount;
            $data['owner_compensate'] = $owner_payment_amount;    
        }
        $data['did'] = $did;
        // $data['is_having_last'] = $is_having_last;
        // $data['last_mid'] = $last_mid;
        $data['od_type'] = $od_type;
        $data['modify_reason'] =$modify_reason;
        $data['modify_result'] =$modify_result;
        $data['modify_desc'] =$modify_desc;
        $data['payment_amount'] =$payment_amount;
        $data['account_name'] =$account_name;
        $data['bank_account'] =$bank_account;
        $data['bank_name'] =$bank_name;
        $data['emp_id'] = $emp_id;
        $res = $dis->DisputePass($data);
        if(!$res){
            $this->helpJsonResult(400, '处理失败');
        }else{
            $this->helpJsonResult(200, '处理成功');
        }
    }
    // 争议审核-驳回
    public function disputeReject(){
        $did = I('get.did');
        if ($did < 1) {
            $this->helpJsonResult(500, '争议id不能为空');
        }
        $dis = new Dispute($did);
        $disInfo = $dis ->detail();
        if ($disInfo['status'] > 1) {
            $this->helpJsonResult(500, '争议数据已处理');
        }
        $o = new AuOrder($disInfo['order_id']);
        $orderInfo = $o ->detail();
        // if ($orderInfo['status'] ==12) {
        //     $this->helpJsonResult(500, '争议数据异常');
        // }
        $reject_result = I('post.reject_result');
        $data = [];
        $data['reject_result'] = $reject_result;
        $data['emp_id']        = $this->emp_id;
        $res = $dis->DisputeReject($data);
        if(!$res){
            $this->helpJsonResult(400, '处理失败');
        }else{
            $this->helpJsonResult(200, '处理成功');
        }
    }
    // 争议审核-违约
    public function disputeViolate(){
        $did = I('get.did');
        if ($did < 1) {
            $this->helpJsonResult(500, '争议id不能为空');
        }
        $dis = new Dispute($did);
        $disInfo = $dis ->detail();
        if ($disInfo['status'] > 1) {
            $this->helpJsonResult(500, '争议数据已处理');
        }
        $o = new AuOrder($disInfo['order_id']);
        $orderInfo = $o ->detail();
        // if ($orderInfo['status'] ==12) {
        //     $this->helpJsonResult(500, '争议数据异常');
        // }
        $data = [];
        $od_type = I('post.od_type');
        // $is_having_last = I('post.is_having_last');
        // $last_mid = I('post.last_mid');
        $modify_result = 2;
        $modify_reason = I('post.modify_reason');
        $modify_desc = I('post.modify_desc');
        $payment_amount = I('post.payment_amount/d',0); //车商赔付金额
        $account_name = I('post.account_name');
        $bank_account = I('post.bank_account');
        $bank_name = I('post.bank_name');
        $emp_id = $this->emp_id;

        if ($modify_reason == 3) {
            $data['dealer_deduction'] = $payment_amount;   
        }
        if ($modify_reason == 4) {
            $owner_payment_amount = I('post.owner_payment_amount/d',0);
            $data['owner_payment_amount'] = $owner_payment_amount;
            $data['owner_deduction'] = $owner_payment_amount;
            $data['dealer_compensate'] = $payment_amount;
        }
        $data['did'] = $did;
        // $data['is_having_last'] = $is_having_last;
        // $data['last_mid'] = $last_mid;
        $data['od_type'] = $od_type;
        $data['modify_reason'] =$modify_reason;
        $data['modify_result'] = $modify_result;
        $data['modify_desc'] =$modify_desc;
        $data['payment_amount'] =$payment_amount;
        $data['account_name'] =$account_name;
        $data['bank_account'] =$bank_account;
        $data['bank_name'] =$bank_name;
        $data['emp_id'] = $emp_id;
        $res = $dis->DisputeViolate($data);
        if(!$res){
            $this->helpJsonResult(400, '处理失败');
        }else{
            $this->helpJsonResult(200, '处理成功');
        }
    }
}